package com.wawei.seeds;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.WalnutRegAuthServiceHandler;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.RestAdapter;

/**
 * Created by itachi on 15/8/7.
 */
public class WalnutResetPasswordActivity extends Activity {
    @InjectView(R.id.walnut_logo)View backView;
    @InjectView(R.id.walnut_reg_submit)View submitView;
    @InjectView(R.id.walnut_get_code)TextView getCodeView;

    @InjectView(R.id.walnut_reg_mobile)EditText mobileView;
    @InjectView(R.id.walnut_reg_alias)EditText codeView;
    @InjectView(R.id.walnut_reg_password)EditText passwordView;

    private TimeCount timer;

    private static final int GET_CODE = 0x01;

    private WalnutRegAuthServiceHandler serviceHandler;
    private ResetNotifyHandler notifyHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_reset);

        ButterKnife.inject(this);

        serviceHandler = new WalnutRegAuthServiceHandler();
        notifyHandler = new ResetNotifyHandler(this);

        timer = new TimeCount(60 * 1000, 1000);

        init();
    }

    private void init() {
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WalnutResetPasswordActivity.this.finish();
            }
        });
        getCodeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCode();
            }
        });
        submitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void getCode() {
        // timer.start();
        String mobile = mobileView.getText().toString();

        if (mobile.length() != 11) {
            Toast.makeText(this, "请填写正确的手机号码!", Toast.LENGTH_LONG).show();
            return;
        }

        getCodeView.setClickable(false);
        getCodeView.setEnabled(false);

        RestAdapter restAdapter = RetrofitNetworkService.makeAsyncRestAdapter();
        serviceHandler.getCode(restAdapter, mobile, notifyHandler, GET_CODE);
        timer.start();
    }

    private void submit() {

    }

    private class TimeCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getCodeView.setClickable(false);
            getCodeView.setEnabled(false);
            getCodeView.setText((millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            getCodeView.setText("获取验证码");
            getCodeView.setEnabled(true);
            getCodeView.setClickable(true);
        }
    }

    private static class ResetNotifyHandler extends WalnutBaseNofityHandler<WalnutResetPasswordActivity, HeroInfo> {

        public ResetNotifyHandler(WalnutResetPasswordActivity activity) {
            super(activity);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            switch (msg.what){
                case GET_CODE:
                    get().getCodeView.setClickable(true);
                    get().getCodeView.setEnabled(true);

                    Bundle bundle = msg.getData();
                    String ok = (String)bundle.getSerializable(RetrofitNetworkService.RESULT_DATA);
                    if ("ok".equals(ok)) {
                        get().timer.start();
                    } else {
                        Toast.makeText(get(), "获取验证码错误", Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    }
}
